package web

import (
	"gddgame.cc/galaxy/binocle"
	"gddgame.cc/galaxy/server/composer"
)

var (
	Plugins = []PluginProvider{}
)

type PluginProvider struct {
	Name    string
	Depends []string

	OnInit     func(builder composer.Builder, core binocle.CoreClient) error
	BindRouter func(builder composer.Builder, core binocle.CoreClient, r *SwaggerRouter)
}

func RegisterPlugin(plugin PluginProvider) {
	// todo 判断依赖关系
	Plugins = append(Plugins, plugin)
}

func BindPlugin(builder composer.Builder, core binocle.CoreClient, r *SwaggerRouter) error {
	for index := range Plugins {
		// load plugin
		if Plugins[index].OnInit != nil {
			if err := Plugins[index].OnInit(builder, core); err != nil {
				return err
			}
		}
	}
	for index := range Plugins {
		// load plugin
		if Plugins[index].BindRouter != nil {
			Plugins[index].BindRouter(builder, core, r)
		}
	}
	return nil
}
